﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.IO;

namespace JustDoit.Impl
{
    public class Helper
    {
        #region    //为double类型的d截取s位小数，且不进行四舍五入操作
        public static double ToFixed(double d, int s)
        {
            double sp = Math.Pow(10, s);
            if (d < 0)
                return Math.Truncate(d) + Math.Ceiling((d - Math.Truncate(d)) * sp) / sp;
            else
                return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
        }
        #endregion

        

        //--Generate a random number--And Check the number

        private static char[] constantString =   
        {   
            '0','1','2','3','4','5','6','7','8','9',  
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',   
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'   
        };

        private static char[] constantNumber = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        public static string GenerateRandomNumber()
        {
            //string dNow = DateTime.Now.ToString().Trim().Replace("-", "").Replace(":", "").Replace(" ", "");
            string NoDate = DateTime.Now.ToString("yyMMdd");
            int Length = 6;

            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constantString[rd.Next(62)]);
            }
            string No = "N" + NoDate + newRandom.ToString();

            return No;

        }

        public static string RandomNumberToName()
        {
            int Length = 12;

            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(10);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constantNumber[rd.Next(10)]);
            }
            string No = newRandom.ToString();
            return No;

        }

        public static void WriteSysLog(string sm)
        {
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\\SysLog\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt"))
            {
                StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\\SysLog\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt", true);
                sw.WriteLine("=======================================================================================");
                sw.WriteLine("Time:" + DateTime.Now.ToString("yyyyMMdd") + ": Message:" + sm);
                sw.WriteLine("=======================================================================================");
                sw.WriteLine("\r");
                sw.Close();
            }
            else
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\\SysLog");
                StreamWriter sw2 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\\SysLog\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt", true);
                sw2.WriteLine("=======================================================================================");
                sw2.WriteLine("Time:" + DateTime.Now.ToString("yyyyMMdd") + ": Message:" + sm);
                sw2.WriteLine("=======================================================================================");
                sw2.WriteLine("\r\n");
                sw2.Close();
            }
        }

    }
}
